﻿using Furion.TaskScheduler;
using LocalNas.Application.Service.Common.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LocalNas.Application.Service
{
    [ApiDescriptionSettings(ConstSwaggerGroup.NAS平台, Name = "公共服务", Order = 100)]
    public class CommonService : IDynamicApiController, ITransient
    {
        /// <summary>
        /// 获取所有任务
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("/Common/GetWorkers")]
        public List<GetWorkersResult> GetWorkers()
        {
            var workers = SpareTime.GetWorkers()
                .Select(x => new GetWorkersResult
                {
                    WorkerName = x.WorkerName,
                    StatusEnum = x.Status,
                    Description = x.Description,
                    Enabled = x.Enabled
                }).ToList();
            var a = SpareTime.GetWorker("秀人美女网");
            return workers;
        }

        /// <summary>
        /// 开始任务
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("/Common/StartWorker")]
        public List<GetWorkersResult> StartWorker(string workerName)
        {
            var worker = SpareTime.GetWorker(workerName);
            if (worker == null) throw Oops.Oh("任务不存在");
            worker.Start();
            return GetWorkers();
        }
    }
}
